import Toast from '@vant/weapp/toast/toast';
import {BASE_URL} from "../../utils/util";

Page({
  data: {
    number: '',
    parentName: '',
    relationship: '',
    phone: '',
    name: '',
    sex: '',
    birthday: '',
    nation: '',
    address: '',
    school: '',
    store: '',
    date: '',
    leftBefore: '',
    leftBeforeAstigma: '',
    rightBefore: '',
    rightBeforeAstigma: '',
    leftAfter: '',
    rightAfter: '',
    amblyopiaMsg: '',
    strabismusMsg: '',
    mydriasisMsg: '',
    glassesMsg: '',
    orthokMsg: '',
    otherMsg: '',
    maternalInher: '',
    paternalInher: '',
    note: '',
    registrant: '',
    regisTime: '',
    storeIndex: getApp().store
  },
  // 事件处理函数
  onLoad(query: Record<string, string | undefined>): void | Promise<void> {
    if (query.path) {
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      });
      const _that = this
      wx.uploadFile({
        url: `${BASE_URL}/health-record/ocr`,
        filePath: query.path as string,
        name: 'file',
        success({data}) {
          const response = JSON.parse(data)
          _that.setData(response.data)
          Toast.clear();
        }
      })
    }

  },

  onSubmit() {
    if (!this.data.number) {
      Toast({
        message: '请填写完整哦',
        duration: 1000
      });
      return
    }
    wx.request({
      url: `${BASE_URL}/health-record`,
      data: this.data,
      method: "POST",
      success({data}) {
        if ((data as any)?.code === 200) {
          Toast.success({
            message: '提交成功',
            duration: 1000
          });
          setTimeout(() => {
            wx.redirectTo({
              url: "../camera/index"
            })
          }, 1000)
        }
      }
    })
  }
})
